<template>
  <div class="mypublish">
    <van-nav-bar
      title="我的发布"
      left-arrow
      @click-left="$router.back()"
    ></van-nav-bar>

    <!-- 发布 -->
    <div class="fa">
      <li
        v-for="(item, index) in mypublishlist"
        :key="item._id"
        @click="
          $router.push({
            path: '/article',
            query: { id: item._id, title: item.title },
          })
        "
      >
        <p>{{ item.title }}</p>
        <div
          :class="{
            none: item.poster_type == 1,
            articlesingleImg: item.poster_type == 2,
            articlemoreImg: item.poster_type == 3,
          }"
        >
          <img :src="img" v-for="img in item.imageSrc" />
        </div>
        <p>{{ item.time }}</p>
        <van-icon name="delete-o" size="25" class="shanchu" @click="onclick(index)" />
      </li>
    </div>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in mypublish" :key="item._id" />
    </van-list>
  </div>
</template>

<script>
import { Tab, Tabs, List, PullRefresh } from "vant";

import { mypublish } from "@/api/user.js";
import { userArticle } from "@/api/user.js";

import { mapState } from "vuex";
import { Dialog } from "vant";

export default {
  name: "mypublish",
  components: {
    [Tab.name]: Tab,
    [Tabs.name]: Tabs,
    [List.name]: List,
    [PullRefresh.name]: PullRefresh,
  },
  data() {
    return {
      mypublishlist: [],
      skip: 0,
      limit: 10,
      list: [],
      loading: false,
      finished: false,
    };
  },

  computed: {
    ...mapState(["uid", "userInfo"]),
  },

  created() {
    this.mypublish();
  },
  methods: {
    mypublish() {
      let { uid } = this;
      mypublish({
        uid,
        skip: this.skip,
        limit: this.limit,
      }).then((res) => {
        console.log(res);
        this.mypublishlist = res.data;
      });
    },
    onclick(index) {
      // let{_id}=this.pulish;
      let { uid ,article_id:_id} = this;
      Dialog.confirm({
        title: " 是否删除头条",
        message: "是否删除",
      }).then(() => {
        // on confirm
        userArticle({
          uid,
          _id,
        })
          .then((res) => {
            console.log(res);
            this.mypublishlist.splic(index,1)
          })
          .catch((err) => {
            // on cancel
               console.log(err);
          });
      });
    },
    onLoad() {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      setTimeout(() => {
        for (let i = 0; i < 10; i++) {
          this.list.push(this.list.length + 1);
        }

        // 加载状态结束
        this.loading = false;

        // 数据全部加载完成
        if (this.list.length >= 30) {
          this.finished = true;
        }
      }, 1000);
    },
    onRefresh() {
      // 清空列表数据
      this.finished = false;

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true;
      this.onLoad();
    },
  },
};
</script>

<style lang="less" scoped>
.shanchu {
  left: 350px;
}
.fa {
  .home-content-list-bto {
    display: flex;
    justify-self: start;
  }
}
.fa {
  overflow: hidden;
  img {
    max-width: 50%;
  }
}
</style>